package com.team.abs.service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.stereotype.Service;
@Service
public class UploadService {
	  // 本地上传
    public String uploadFileToNginx2(byte[] file, String fix) {

        String ROOT_PATH = "D:\\img\\";
        File targetFile = new File(ROOT_PATH);
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }
        FileOutputStream out_main = null;
        FileOutputStream out_copy = null;
        SimpleDateFormat sm=new SimpleDateFormat("yyyy_MM_DD_HH_mm_ss");
        //一张用于存储 一张用于固定比对 该模块没有对并发进行处理
        String fileName_main = sm.format(new Date(System.currentTimeMillis()));
        String fileName_copy = "target";
        try {
        	out_copy = new FileOutputStream(ROOT_PATH + fileName_copy + fix);
        	out_copy.write(file);
        	out_copy.flush();
        	out_copy.close();
        	out_main = new FileOutputStream(ROOT_PATH + fileName_main + fix);
        	out_main.write(file);
        	out_main.flush();
        	out_main.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fileName_copy + fix;
    }

    public String uploadImage(byte[] file, String fix) {
        String path = uploadFileToNginx2(file, fix);
        
        return path;
    }
    
}
